package com.maxd.service.wx;


import com.maxd.model.CommonInfo;
import com.maxd.respository.common.CommonRepository;
import com.maxd.utils.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weixin.popular.api.TicketAPI;
import weixin.popular.support.TokenManager;

/**
 * @author zhangd
 */
@Slf4j
@Service
public class AccountTokenServiceImpl implements AccountTokenService {
    @Autowired
    private CommonRepository commonRepository;

    @Override
    public String getWxToken() {
        try {
            //微信appid
            CommonInfo one = commonRepository.findOne(5);
            return TokenManager.getToken(one != null ? one.getValue() : Config.appId);
        } catch (Exception e) {
            throw new RuntimeException("GET_ACCESS_TOKEN_FAIL");
        }
    }

    @Override
    public String getMpToken() {
        try {
            //微信appid
            CommonInfo one = commonRepository.findOne(45);
            return TokenManager.getToken(one != null ? one.getValue() : Config.appId);
        } catch (Exception e) {
            throw new RuntimeException("GET_ACCESS_TOKEN_FAIL");
        }
    }

    @Override
    public String getJsApiTicket(String accessToken) {
        return TicketAPI.ticketGetticket(accessToken).getTicket();
    }

}
